package com.chenyuxin.ilp.util;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class TimeUtil {
    public static LocalDateTime currentTimeMillisToLocalDateTime(Long currentTimeMillis){
        return Instant.ofEpochMilli(currentTimeMillis)
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }
    public static Long localDateToCurrentTimeMillis(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();

    }
    public static String LocalDateTimeToString(LocalDateTime localDateTime) {
        LocalDate today = LocalDate.now();
        LocalDateTime now = LocalDateTime.now();
        LocalDate date = localDateTime.toLocalDate();
        LocalTime time = localDateTime.toLocalTime();

        // 格式化时分
        String timeStr = String.format("%02d:%02d", time.getHour(), time.getMinute());

        if (date.equals(today)) {
            return "今天" + timeStr;
        } else if (date.equals(today.plusDays(1))) {
            return "明天" + timeStr;
        } else if (date.getYear() == today.getYear()) {
            if (date.getMonth() == today.getMonth()) {
                // 本月其他日子
                return date.getDayOfMonth() + "日" + timeStr;
            } else {
                // 本年其他月份
                return date.getMonthValue() + "月" + date.getDayOfMonth() + "号" + timeStr;
            }
        } else {
            // 其他年份
            return date.getYear() + "年" + date.getMonthValue() + "月" + date.getDayOfMonth() + "日" + timeStr;
        }
    }
    public static String getTimeDiffDescription(LocalDateTime targetTime) {
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(now, targetTime); // 可以是正也可以是负

        long minutes = Math.abs(duration.toMinutes());
        long hours = minutes / 60;
        long days = hours / 24;

        if (minutes < 60) {
            return minutes + "分钟";
        } else if (hours < 24) {
            return hours + "小时";
        } else {
            return days + "天";
        }
    }

}
